import {request, writeLog} from "../libs/core.js"
export default {
  namespaced: true,  //多出的一行
  state: {
  	data: {
      sysOrg: [],
      sysUser: [],
      sysRole: [],
      sysTables: [],
      sysModule: [],
	  sysConfig: {},
      yesOrNo: {1:"是", 2:"否"},
      verOsType: {1:"Android", 2:"ios"},
      sysLogType: {1:'错误', 2:'警告', 3:'调试', 4:'其他'},
      checkStatus: {1:"审核通过", 2:"拒绝通过", 3:"待审核"},
      saleStatus: {1:"上架", 2:"下架", 3:"待审核", },
      upfileType: {1:'pdf', 2:'chm', 3:'doc', 4:'ppt', 5:'rar', 6:'img', 8:'视频'},
      prjServerRole: {1:'测试', 2:'仿真', 3:'正式'},
      prjOfficerRole: {1:'开发', 2:'策划', 3:'运营', 4:'QA', 5:'运维'},
      sysRoleStatus: {1:'正常', 2:'禁用'},
      sysUserStatus: {1:'正常', 2:'锁定', 3:'禁用'},
      sysUserSex: {1:'男', 2:'女', 0:'保密'},
      sysPayStatus: {1:'已付款', 2:'已退款', 3:'退款中', 4:'申请退款', 9:'待付款'},
      sysPayType: {1:'微信',2:'支付宝',3:'QQ',4:'转账'},
      sysShipStatus: {1:'已发货',2:'签收', 9:'待发货'}
    }
  },
  mutations: {
    initSet(state, datas) {
      for (let keys in datas) {
        state.data[keys] = datas[keys];
      }
    }
  },
  getters: {
  	list: state => {
      var dicts = {};
      for (var kstr in state.data) {
        dicts[kstr] = [];
        if (Array.isArray(state.data[kstr])) {
          dicts[kstr] = state.data[kstr];
          continue;
        }
        for (var str in state.data[kstr]) {
          (/^[\d]+$/.test(str)) && (str = Number(str));
          dicts[kstr].push([str, state.data[kstr][str]]);
        }
      }
      return dicts;
  	},
  },
  actions: {
	async shareDicts(context, args) {
		try {
			(!args) && (args = {key: 'sysConfigDicts'});
			const data = await request({url:'/api/core/config/dict', data:args, method:'post'});
			context.commit("initSet", data.datas);
		} catch(e) {
			writeLog(0, e);
		}
		return false;
    },
	async shareConfig(context, args) {
	  try {
	    const data = await request({url:'/api/core/share/configs', data:args, method:'post'});
	    if (data && data.code == 0) {
            context.commit("initSet", {sysConfig:data.datas});
        }
	  } catch(e) {
	    writeLog(0, e);
	  }
	  return false;
	},
    async sysOrg(context, args) {
      try {
        const data = await request({url:'/api/core/share/sysorg', data:args, method:'post'});
        if (data.code == 0 && data.datas.length > 0) {
            let dataset = [];
            for (let i = 0; i < data.datas.length; i++) {
                dataset.push([data.datas[i].id, data.datas[i].name]);
            }
            context.commit("initSet", {sysOrg:dataset});
        }
      } catch(e) {
        writeLog(0, e);
      }
      return false;
    },
    async sysUser(context, args) {
      try {
        const data = await request({url:'/api/core/share/sysuser', data:args, method:'post'});
        if (data.code == 0 && data.datas.length > 0) {
            let dataset = [];
            for (let i = 0; i < data.datas.length; i++) {
                let name = data.datas[i].name + "["+data.datas[i].nickname+"]"
                dataset.push([data.datas[i].id, name]);
            }
            context.commit("initSet", {sysUser:dataset});
        }
      } catch(e) {
        writeLog(0, e);
      }
      return false;
    },
    async sysRole(context, args) {
      try {
        const data = await request({url:'/api/core/share/sysrole', data:args, method:'post'});
        if (data.code == 0 && data.datas.length > 0) {
            let dataset = [];
            for (let i = 0; i < data.datas.length; i++) {
                dataset.push([data.datas[i].id, data.datas[i].name]);
            }
            context.commit("initSet", {sysRole:dataset});
        }
      } catch(e) {
        writeLog(0, e);
      }
      return false;
    },
    async sysModule(context, args) {
      try {
        const data = await request({url:'/api/core/share/module', data:args, method:'post'});
        if (data.code == 0 && data.datas.length > 0) {
            let dataset = [];
            for (let i = 0; i < data.datas.length; i++) {
                dataset.push([data.datas[i].id, data.datas[i].title]);
            }
            context.commit("initSet", {sysModule:dataset});
        }
      } catch(e) {
        writeLog(0, e);
      }
      return false;
    },
    async sysTable(context, args) {
      try {
        const data = await request({url:'/api/core/share/tables', data:args, method:'post'});
        let tables = {};
        if (data.code == 0 && data.datas.tables.length > 0) {
            for (let idx = 0; idx < data.datas.tables.length; idx++) {
                let table = data.datas.tables[idx];
                tables[table] = table;
            }
            context.commit("initSet", {sysTables:tables});
        }
      } catch(e) {
        writeLog(0, e);
      }
      return false;
    }
  }
}
